package com.zqxx.examine.management.controller.action.admin;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zqxx.examine.management.security.StandardPasswordEncoderForSha1;
import com.zqxx.examine.management.security.UserInfo;
import com.zqxx.examine.management.service.NewsService;
import com.zqxx.examine.management.service.UserService;
import com.zqxx.examine.model.exam.Message;
import com.zqxx.examine.model.news.News;
import com.zqxx.examine.model.user.User;

@Controller
public class SystemActionAdmin
{
  @Autowired
  private UserService userService;
  @Autowired
  private NewsService newsService;
  
  @RequestMapping(value={"/admin/add-admin"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message addUser(@RequestBody User user)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    Message message = new Message();
    if (!userInfo.getAuthorities().toString().contains("ROLE_ADMIN"))
    {
      message.setResult("权限错误！");
      message.setMessageInfo("只有管理员才可以添加管理员！");
      return message;
    }
    user.setCreateTime(new Date());
    String password = user.getPassword() + "{" + user.getUserName().toLowerCase() + "}";
    PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1();
    String resultPassword = passwordEncoder.encode(password);
    user.setPassword(resultPassword);
    user.setEnabled(true);
    user.setCreateBy(userInfo.getUserid());
    user.setUserName(user.getUserName().toLowerCase());
    try
    {
      this.userService.addUser(user, "ROLE_ADMIN", -1, userInfo.getRoleMap());
    }
    catch (Exception e)
    {
      if (e.getMessage().contains(user.getUserName()))
      {
        message.setResult("duplicate-username");
        message.setMessageInfo("重复的用户名");
      }
      else if (e.getMessage().contains(user.getNationalId()))
      {
        message.setResult("duplicate-national-id");
        message.setMessageInfo("重复的身份证");
      }
      else if (e.getMessage().contains(user.getEmail()))
      {
        message.setResult("duplicate-email");
        message.setMessageInfo("重复的邮箱");
      }
      else if (e.getMessage().contains(user.getPhoneNum()))
      {
        message.setResult("duplicate-phone");
        message.setMessageInfo("重复的电话");
      }
      else
      {
        message.setResult(e.getCause().getMessage());
        e.printStackTrace();
      }
    }
    return message;
  }
  
  @RequestMapping(value={"/admin/add-news"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message addUser(@RequestBody News news)
  {
    Message msg = new Message();
    

    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    try
    {
      news.setUserId(userInfo.getUserid());
      this.newsService.addNews(news);
    }
    catch (Exception e)
    {
      msg.setResult(e.getClass().getName());
      e.printStackTrace();
    }
    return msg;
  }
}
